class Solution
{
public:
    int paintWalls(vector<int> &cost, vector<int> &time)
    {
        int n = cost.size();
        vector<int> &count = time;

        for (auto &c : count)
        {
            c += 1;
        }

        // dp[i][count]表示处理完前i组time和cost后, 已经刷了count堵墙的最小总cost
        vector<vector<int>> dp(n, vector<int>(1500, INT_MAX));
        dp[0][0] = 0;
        dp[0][count[0]] = cost[0];
        for (int i = 1; i < n; ++i)
        {
            for (int lastCount = 0; lastCount <= 500; ++lastCount)
            {
                int thisCount = lastCount + count[i];
                if (dp[i - 1][lastCount] != INT_MAX)
                {
                    dp[i][thisCount] = min(dp[i - 1][lastCount] + cost[i], dp[i][thisCount]);
                    dp[i][lastCount] = min(dp[i - 1][lastCount], dp[i][lastCount]);
                }
            }
        }

        int result = INT_MAX;
        for (int i = 0; i < n; ++i)
        {
            for (int totalCount = n; totalCount < 1500; ++totalCount)
            {
                result = min(result, dp[i][totalCount]);
            }
        }
        return result;
    }
};